/*
    This file is part of Emu.

    Emu is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Emu is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with Emu.  If not, see <http://www.gnu.org/licenses/>. */
#include <QSettings>
#include <QDebug>

#include "eglobalprefs.h"

EGlobalPrefs::~EGlobalPrefs()
{
    /* The original will delete the copy, the copy will do nothing. */
    if (_initial)
        delete _initial;
}

EGlobalPrefs *EGlobalPrefs::loadPrefs()
{
    QSettings settings(QSettings::IniFormat, QSettings::UserScope, "emu",
                       "emu");

    /* History :
       prefVersion - 1.0 - Ancient, when Emu was at Sourceforge.
       prefVersion - 1.1 - Still old, still not supported.
       prefVersion - 1.2 - All colors in prefs, plus window sizes and session info.
       version     - 1.0 - Colors are session specific, instead of in prefs.
       version     - 2.0 - Removed autoscroll. */
    QString version = settings.value("version", "2.0").toString();

    EGlobalPrefs *prefs = new EGlobalPrefs;

    settings.beginGroup("geometries");
    prefs->setViewerSize(settings.value("viewerSize", QSize(0, 0)).toSize());
    prefs->setViewerPos(settings.value("viewerPos", QPoint(0, 0)).toPoint());
    settings.endGroup();

    settings.beginGroup("session");
    prefs->setHandle(settings.value("splitHandle", QByteArray()).toByteArray());
    settings.endGroup();

    QFont f;
    settings.beginGroup("font");
    f.setFamily(settings.value("family", "Courier New [Monospace]").toString());
    f.setPointSize(settings.value("size", 8).toInt());
    f.setBold(settings.value("bold", false).toBool());
    f.setItalic(settings.value("italic", false).toBool());
    settings.endGroup();

    prefs->setFont(f);

    /* Make a copy of the current preference state, to check for later. */
    prefs->_initial = NULL;
    prefs->_initial = new EGlobalPrefs(*prefs);

    return prefs;
}

void EGlobalPrefs::savePrefs(EGlobalPrefs *prefs)
{
    if (!prefs->modified())
        return;

    QSettings settings(QSettings::IniFormat, QSettings::UserScope, "emu",
                       "emu");

    settings.beginGroup("geometries");
    settings.setValue("viewerSize", prefs->viewerSize());
    settings.setValue("viewerPos", prefs->viewerPos());
    settings.endGroup();

    settings.beginGroup("session");
    settings.setValue("splitHandle", prefs->handle());
    settings.endGroup();

    QFont f = prefs->font();

    settings.beginGroup("font");
    settings.setValue("family", f.family());
    settings.setValue("size", f.pointSize());
    settings.setValue("bold", f.bold());
    settings.setValue("italic", f.italic());
    settings.endGroup();

    settings.setValue("version", "2.0");

    settings.sync();

}

bool EGlobalPrefs::modified()
{
    return !(_handle == _initial->handle() &&
             _viewerSize == _initial->viewerSize() &&
             _viewerPos == _initial->viewerPos() &&
             _font == _initial->font());
}
